using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MMM.Core.Domain;
using ServiceStack.ServiceHost;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using MMM.Services.Base;
using MMM.Services.Operations;
using MMM.Services.Type;
using MMM.Services.Response;
using MMM.Core;
using ServiceStack.ServiceInterface;

namespace MMM.Services
{
    /// <summary>
    /// 行政区域管理 基础服务类(POST,PUT)
    /// </summary>
    public class AdministrativeAreaService : BaseService<AdministrativeArea>
    {
        public override object OnGet(AdministrativeArea request)
        {
            IList<AdministrativeArea> lst = new List<AdministrativeArea>();
            if (request != null)
            {
                switch (request.Level)
                {
                    case 2: //市
                        if (request.Province.EndsWith("市"))
                        {
                            lst.Add(new AdministrativeArea { Level=2, City="区", Province=request.Province });
                        }
                        else
                        {
                            lst = Rep.GetAll(t => t.Province == request.Province && t.Level == 2).OrderBy(T => T.Code).ToList();
                        }
                        break;
                    case 3: //县
                        lst = Rep.GetAll(t => t.Province == request.Province && t.City == request.City && t.Level == 3).OrderBy(t => t.Code).ToList();
                        break;
                    default: //省1
                        lst = Rep.GetAll(t => t.Level == 1).OrderBy(t => t.Code).ToList();
                        break;
                }
               
            }
            return new OperationResult<AdministrativeArea>(lst, lst.Count());
        }
    }
    /// <summary>
    /// 行政区域管理 服务类(GET)
    /// </summary>
    public class AdministrativeAreaGetSerice : BaseGetService<AdministrativeAreaPageGet, AdministrativeArea>
    {
    }
    /// <summary>
    /// 行政区域管理 服务类(DELETE)
    /// </summary>
    public class AdministrativeAreaDeleteService : BaseDeleteService<AdministrativeArea, AdministrativeAreaDelete>
    {
    }

    /// <summary>
    /// 行政区域管理 树节点操作
    /// </summary>
    public class CategoryNodesService : BaseRestService<AreaNode>
    {

        //IRepository<AdministrativeArea> pcRep = ApplicationContext.Container.GetInstance<IRepository<AdministrativeArea>>();
        public override object OnGet(AreaNode request)
        {
            var cache = this.GetCacheClient();
            string key = "areanode" + request.pLevel;
            var pnode= cache.Get<AreaNode>(key);
            if (pnode!=null)
            {
                return pnode;
            }
            var rep = GetRepository<AdministrativeArea>();
            pnode = new AreaNode() {
                id=Guid.Empty, Code="000000", pLevel=0, leaf=false, expanded=true
            };

            var lst = rep.GetAll().OrderBy(t => t.Code);
            addProvince(lst.ToList(), pnode, request.pLevel);

            cache.Add<AreaNode>(key, pnode);
            return pnode;
        }
        
        private void addProvince(IList<AdministrativeArea> lst, AreaNode rootNode,int level=1)
        {
            IEnumerable<AdministrativeArea> cateList = lst.Where(t => t.Level == 1);
            
            foreach (var item in cateList)
            {
                var isLeaf = true;
                var node = new AreaNode { id = item.Id, leaf = isLeaf, expanded=false, text = item.Province, level=2, pLevel = item.Level, Code = item.Code, Spell = item.Spell };
                rootNode.children.Add(node);
                if (level > 1)
                {
                    addCity(lst, node, level);
                }
            }
        }

        private void addCity(IList<AdministrativeArea> lst, AreaNode rootNode, int level = 2)
        {
            if (rootNode.text.EndsWith("市"))
            {
                rootNode.leaf = true;
                //addCounty2(lst, rootNode);
                return;
            }

            IEnumerable<AdministrativeArea> cateList = lst.Where(t => t.Level == 2 && t.Province==rootNode.text);
            if (cateList.Count()>0)
            {
                rootNode.leaf = false;
            }
            foreach (var item in cateList)
            {
                var isLeaf = true;
                var node = new AreaNode { id = item.Id, leaf = isLeaf, text = item.City, level = 3, pLevel = item.Level, Code = item.Code, Spell = item.Spell };
                rootNode.children.Add(node);
                if (level > 2)
                {
                    addCounty(lst, node);
                }
            }
        }
        //加载直辖市
        private void addCounty2(IList<AdministrativeArea> lst, AreaNode rootNode)
        {
            IEnumerable<AdministrativeArea> cateList = lst.Where(t => t.Level == 3 && t.Province == rootNode.text);
            if (cateList.Count() > 0)
            {
                rootNode.leaf = false;
            }
            foreach (var item in cateList)
            {
                var isLeaf = true;
                var node = new AreaNode { id = item.Id, leaf = isLeaf, text = item.County, level = 4, pLevel = item.Level, Code = item.Code, Spell = item.Spell };
                rootNode.children.Add(node);
            }
        }

        private void addCounty(IList<AdministrativeArea> lst, AreaNode rootNode)
        {
            IEnumerable<AdministrativeArea> cateList = lst.Where(t => t.Level == 3 && t.City==rootNode.text);
            if (cateList.Count() > 0)
            {
                rootNode.leaf = false;
            }
            foreach (var item in cateList)
            {
                var isLeaf = true;
                var node = new AreaNode { id = item.Id, leaf = isLeaf, text = item.County, level = 4, pLevel = item.Level, Code = item.Code, Spell = item.Spell };
                rootNode.children.Add(node);
            }
        }

        ///// <summary>
        ///// 删除树节点
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //public override object OnDelete(CategoryNodes request)
        //{
        //    pcRep.DeleteByID(request.id);
        //    return OperationResult<CategoryNodes>.SuccessRespose;
        //}
    }
}
